import {
	isAbsolutePath, serializeGetParams
} from '../../util'



export function xhrGet(config) {
    const xhr = new XMLHttpRequest()
    let headers = {
        ...config.headers,
    }
    console.log('xhr-----config---', config)
    return new Promise((resove, reject) => {
        const _serializeParams = config.data && Object.keys(config.data).length > 0 ? serializeGetParams(config.data) : ''
        const _url = isAbsolutePath(config.url) ? config.url.concat(_serializeParams) : config.baseURL.concat(config.url, _serializeParams)
        xhr.open(config.method, _url, true)

        if (headers) {
            Object.keys(config.headers).map(name => {
                xhr.setRequestHeader(name, config.headers[name])
            })
        }
        xhr.timeout = config.timeout
        xhr.ontimeout = (e) => {
            reject(e)
            console.error('The request has timed out')
        }
        xhr.onerror = function (params) {
            console.error('The request on err')
            reject(params)
        }
        xhr.onreadystatechange = function () {
            console.log(xhr)
            if (xhr.readyState === 4) {
                if (xhr.status === 200 || xhr.status === 304) {
                    let response = JSON.parse(xhr.responseText)
                    console.log(xhr.responseText)
                    resove(response)
                }
            } else {
                return
            }
        }
        xhr.onabort = function (params) {
            if (!xhr) return
        }
        if (config.withCredentials) {
            xhr.withCredentials = true
        } else {
            xhr.withCredentials = false
        }
        xhr.send(null)
    })
}
export function xhrPost(config) {
    const xhr = new XMLHttpRequest()
    let headers = {
        ...config.headers,
    }
    return new Promise((resove, reject) => {
        const _url = isAbsolutePath(config.url) ? config.url : config.baseURL.concat(config.url)
        xhr.open(config.method, _url, true)
        if (headers) {
            Object.keys(config.headers).map(name => {
                xhr.setRequestHeader(name, config.headers[name])
            })
        }
        xhr.timeout = config.timeout
        xhr.ontimeout = (e) => {
            reject(e)
            console.error('The request has timed out')
        }
        xhr.onerror = function (params) {
            reject(params)
        }
        xhr.onreadystatechange = function (e) {
            if (xhr.readyState === 4) {
                if (xhr.status === 200 || xhr.status === 304) {
                    let response = JSON.parse(xhr.responseText)
                    resove(response)
                } else {
                    // let err = {
                    //     status: xhr.status,
                    //     message: xhr.statusText
                    // }
                    reject(e)
                }
            }
        }
        if (config.withCredentials) {
            xhr.withCredentials = true
        } else {
            xhr.withCredentials = false
        }
        xhr.send(JSON.stringify(config.data))
    })
}